package top.codedance.iotp.serialPortSDK.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class FloatUtil {

    /**
     * 串口时间转换 001
     *
     * @param msg
     * @return
     */
    public static String timeToStr_001(String msg) {
        if (msg.length() < 10) {
            throw new IllegalArgumentException("数据长度不符合规则！");
        }
        String year = String.valueOf(HexUtil.HexToInt(msg.substring(0, 2)));
        String month = String.valueOf(HexUtil.HexToInt(msg.substring(2, 4)));
        String day = String.valueOf(HexUtil.HexToInt(msg.substring(4, 6)));
        String h = String.valueOf(HexUtil.HexToInt(msg.substring(6, 8)));
        String m = String.valueOf(HexUtil.HexToInt(msg.substring(8, 10)));
        String s = ":00";
        if (msg.length() == 12) {
            s = ":"+ msg.substring(10,12);
        }

        String str = "20" + year + "-" + (month.length() == 1 ? "0" + month : month) + "-" + (day.length() == 1 ? "0" + day : day) + " " + (h.length() == 1 ? "0" + h : h) + ":" + (m.length() == 1 ? "0" + m : m) + s;
        return str;
    }

    /**
     * 串口时间转换 002
     *
     * @param msg
     * @return
     */
    public static String timeToStr_002(String msg) {
        if (msg.length() < 14) {
            throw new IllegalArgumentException("数据长度不符合规则！");
        }
        Integer year = HexUtil.HexToInt(msg.substring(0, 4));
        Integer month = HexUtil.HexToInt(msg.substring(4, 6));
        Integer day = HexUtil.HexToInt(msg.substring(6, 8));
        Integer h = HexUtil.HexToInt(msg.substring(8, 10));
        Integer m = HexUtil.HexToInt(msg.substring(10, 12));
        Integer s = HexUtil.HexToInt(msg.substring(12, 14));
        String str = year + "-" + (month < 10 ? "0" + month : month) + "-" + (day < 10 ? "0" + day : day) + " " + (h < 10 ? "0" + h : h) + ":" + (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s);
        return str;
    }

    /**
     * 串口时间转换成日期
     * @Description
     * @Param msg
     * @return java.lang.String
     * @author ChenPeng
     * @date 2023/4/12 22:39
     */
    public static Date timeToData(String msg) {
        if (msg.length() < 10) {
            throw new IllegalArgumentException("数据长度不符合规则！");
        }
        String year = String.valueOf(HexUtil.HexToInt(msg.substring(0, 2)));
        String month = String.valueOf(HexUtil.HexToInt(msg.substring(2, 4)));
        String day = String.valueOf(HexUtil.HexToInt(msg.substring(4, 6)));
        String h = String.valueOf(HexUtil.HexToInt(msg.substring(6, 8)));
        String m = String.valueOf(HexUtil.HexToInt(msg.substring(8, 10)));
        String s = ":00";
        if (msg.length() == 12) {
            s = ":"+ msg.substring(10,12);
        }

        String str = "20" + year + "-" + (month.length() == 1 ? "0" + month : month) + "-" + (day.length() == 1 ? "0" + day : day) + " " + (h.length() == 1 ? "0" + h : h) + ":" + (m.length() == 1 ? "0" + m : m) + s;
        Date result = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            result = sdf.parse(str);
        } catch (Exception e) {
            throw new IllegalArgumentException("日期格式转换不正确！");
        }
        return result;
    }

    /**
     * 浮点数表示法拆解
     *
     * @param msg
     * @return
     */
    public static String floatTOString(String msg) {
        if (msg.length() < 8) {
            throw new IllegalArgumentException("数据长度不符合规则！");
        }
        //0
        //100 0110 0
        //111 1101 1000 0100 0110 0110
        //十六进制转二进制
        String str = HexUtil.hexTo2String(msg);
        str = str.replace(" ", "");
        String s = str.substring(0, 1);//符号位部分
        String e = str.substring(1, 9);//指数部分
        String m = str.substring(9, str.length());//尾数部分
        //符号位
        String result = s.equals("0") ? "" : "-";
        //指数
        double e1 = Math.pow(2, HexUtil.binary2Decimal(e) - 127);
        //尾数
        double m1 = 1.0 + (HexUtil.binary2Decimal(m) / 8388608.0);
        return result + String.format("%.2f", e1 * new Double(String.format("%.8f", m1)));
    }
}
